#include "VAmpImp.h"

VAmpImp::VAmpImp(wxWindow* parent):ImpCheck(parent)
{
#ifdef HAVE_VAMP_LIBRARY
	FirstAmp = new VAmpLib();
	//FirstAmp = NULL;
	int a = Connect();
	if (a==0)
	{
		connect = true;
		Sleep(500);
		esci = false;
		wxThread::Create();
		Run(); //Run thread
	}
	else
		connect = false;
#endif
}
VAmpImp::~VAmpImp()
{
	
}

#ifdef HAVE_VAMP_LIBRARY
void* VAmpImp::Entry(void)
{
	if (FirstAmp!=NULL) 
	{	
		while (!esci)
		{
			if (this->IsShown())
			{
				if (FirstAmp->Run)
				{
					FirstAmp->GetData();
					CheckImpedance(FirstAmp->Impedance[0],ImpCh1);
					CheckImpedance(FirstAmp->Impedance[1],ImpCh2);
					CheckImpedance(FirstAmp->Impedance[2],ImpCh3);
					CheckImpedance(FirstAmp->Impedance[3],ImpCh4);
					CheckImpedance(FirstAmp->Impedance[4],ImpCh5);
					CheckImpedance(FirstAmp->Impedance[5],ImpCh6);
					CheckImpedance(FirstAmp->Impedance[6],ImpCh7);
					CheckImpedance(FirstAmp->Impedance[7],ImpCh8);
					if (FirstAmp->Channels>9)
					{
						CheckImpedance(FirstAmp->Impedance[8],ImpCh9);
						CheckImpedance(FirstAmp->Impedance[9],ImpCh10);
						CheckImpedance(FirstAmp->Impedance[10],ImpCh11);
						CheckImpedance(FirstAmp->Impedance[11],ImpCh12);
						CheckImpedance(FirstAmp->Impedance[12],ImpCh13);
						CheckImpedance(FirstAmp->Impedance[13],ImpCh14);
						CheckImpedance(FirstAmp->Impedance[14],ImpCh15);
						CheckImpedance(FirstAmp->Impedance[15],ImpCh16);
					}
					CheckImpedance(FirstAmp->Impedance[FirstAmp->Channels-1],ImpRef1);
					this->Layout();
					
				}
				Sleep(100);
			}
		}
	}
	else 
	{
		wxMessageBox(L"Can't connect to Impedance Check",L"Error");
		while (!esci)
		{
			Sleep(100);
		}
		
	}
	SetDisplayImg(_T(".//Media//"),_T("VAmpImg.bmp"));
	if (FirstAmp!=NULL)
	{
		if (FirstAmp->Run)
		{
			FirstAmp->VAmpStop();
			FirstAmp->VAmpClose();
		}
	}
	return NULL;
}
int VAmpImp::Connect(void)
{
	//Connessione a BrainAmp
	if (FirstAmp->CheckDevice()!=0)
		return 1;
	if (FirstAmp->GetInfo()!=0)
		return 1;
	if (FirstAmp->VAmpConnect(dmNormal)!=FA_ERR_OK)
		return 1;
	return 0;
}

void VAmpImp::OnStart(void)
{
	//Set impedance mode
	FirstAmp->SetData(IAC_AT_IMPEDANCE,false);
	Sleep(300);
	//Display Image
	SetDisplayImg(_T(".//Media//"),_T("VAmpImp.bmp"));
	//If VAmp has less than 8 channels, disable ch>9
	if (FirstAmp->Channels<=9)
	{
		NameCh9->Disable();
		ImpCh9->Disable();
		NameCh10->Disable();
		ImpCh10->Disable();
		NameCh11->Disable();
		ImpCh11->Disable();
		NameCh12->Disable();
		ImpCh12->Disable();
		NameCh13->Disable();
		ImpCh13->Disable();
		NameCh14->Disable();
		ImpCh14->Disable();
		NameCh15->Disable();
		ImpCh15->Disable();
		NameCh16->Disable();
		ImpCh16->Disable();
	}
	NameRef2->Disable();
	ImpRef2->Disable();
	NameRef3->Disable();
	ImpRef3->Disable();
	NameRef4->Disable();
	ImpRef4->Disable();
	//Start Acq
	FirstAmp->VAmpStart();
	
}
void VAmpImp::OnStop(void)
{
	esci = true;
	Sleep (300);
}

void VAmpImp::CheckImpedance(unsigned int imp, wxStaticText *Value)
{
	if (imp==NULL)
		return;
	if (imp<1000)
	{
		Value->SetLabel(wxString::Format(_("%d ohm"),imp));
		Value->SetBackgroundColour(wxColour(0,255,0)); //Good
		Value->SetForegroundColour(wxColor(0,0,0));
	}
	else if ((imp/1000) >2000000)
	{
		Value->SetBackgroundColour(wxColor(0,0,0));
		Value->SetForegroundColour(wxColor(255,255,255));
		Value->SetLabel(_T("NC"));
	}
	else
	{
		Value->SetLabel(wxString::Format(_("%d K"),imp/1000));	
		if ((imp/1000)<5)
			Value->SetBackgroundColour(wxColour(0,255,0)); //Good
		else if ((imp/1000)<25)
			Value->SetBackgroundColour(wxColour(255,255,0)); //Medium
		else 
			Value->SetBackgroundColour(wxColour(255,0,0)); //Bad
		Value->SetForegroundColour(wxColor(0,0,0));
	}
	Value->SetSize(70,20);
}

int VAmpImp::SetDisplayImg(wxString FilePath, wxString FileName)
{
	if (FirstAmp!=NULL)
	{
		HBITMAP Bitmap;
		wxString BitMapFile;
		//CARICARE IMMAGINE
		BitMapFile= FilePath+FileName;
		Bitmap = (HBITMAP)LoadImage(NULL, BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
		if (FA_ERR_OK != faSetBitmap(FirstAmp->DeviceId,Bitmap))
			return -1;
		faSetContrast(FirstAmp->DeviceId,100);
		faSetBrightness(FirstAmp->DeviceId,100);
		return 1;
	}
	else 
		return -1;
}
#endif